var rootPath = getRootPath();
var flag = true;

$(function() {
	//检测用户名是否存在、是否合法
	$("#uname").blur(function(){
		var uname = $("#uname").val();
		if(uname != '' && uname != oldUname) {
			if(uname.length > 50) {
				showErrMsg("uname", "昵称输入过长,请精简");
				return;
			}
			$.ajax({
				url: rootPath + '/userFront/verifyUname',
		        type: 'GET',
		        data: {
		        	"uname" : uname
		        },
		        contentType: 'application/json;charset=UTF-8',
		        success: function (data) {
		        	if(data.returnCode == '0000') {
		        		flag = true;
		        	} else if ("1001" == data.returnCode){
		        		showErrMsg("uname",data.returnObject);
		        		flag = false;
		        	} else if("1004" == data.returnCode){
		        		showErrMsg("uname","昵称不符合规范!");
		        		flag = false;
		        	} else {
		        		flag = false;
		        		layer.alert(data.returnMessage);
		        	}
		        },
		        error: function (jqXHR, textStatus, errorThrown) {
		        	flag = false;
		        	layer.alert("error");
		        }
		    });
		}
	});
	
	$("#save").click(function() {
		if(!checkData() || !flag){
			return;
		}
		
		if($("#uname").val().length > 50) {
			showErrMsg("uname", "昵称输入过长,请精简");
			return;
		}
		
		if($("#intro").val().length > 200) {
			showErrMsg("intro", "个人简介输入过长,请精简");
			return;
		}
		
		//处理数据
		var jsonObject = JSON.parse(JSON.stringify($('#basicInfoForm').serializeJson()));
		var json;
		if(jsonObject.hasOwnProperty("uname")) {
			json = JSON.stringify($('#basicInfoForm').serializeJson());
		} else {
			jsonObject.uname = $("#uname").val();
			json = JSON.stringify(jsonObject);
		}
		
		showLoading();
		$.ajax({
			url: rootPath + '/userFront/updateBasicInfo',
	        type: 'POST',
	        data: json,
	        contentType: 'application/json;charset=UTF-8',
	        success: function (data) {
	        	hideLoading();
	        	if(data.returnCode == '0000') {
	        		oldUname = $("#uname").val();
	        		layer.msg("保存成功",{icon:6});
	        	} else {
	        		layer.alert(data.returnMessage);
	        	}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	hideLoading();
	        	layer.alert("error");
	        }
	    });
	});
});